﻿Imports System.Net
Imports System.Threading

Module Lyrics
    Private CheckConn As New CheckInternetConnection()
    Public Function GetLyrics(ByVal context As TagLib.File, Optional ByVal Fetch As Boolean = False, Optional ByVal Artist As String = Nothing, Optional ByVal Title As String = Nothing) As String
        If context Is Nothing = False Then
            If My.Settings.LyricsService = "LYRDB" And CheckConn.IsConnected = True Then
                Try
                    Dim queryUrl As String = [String].Format("http://webservices.lyrdb.com/lookup.php?q={0}|{1}&for=match", Artist, Title)
                    Dim client As New WebClient()
                    Dim result As String = client.DownloadString(queryUrl)
                    If result = [String].Empty Then
                        Return [String].Empty
                    End If

                    For Each x As String In result.Split(ControlChars.Lf)
                        Dim id As String = x.Split("\"c)(0)
                        Dim lyricsUrl As New Uri("http://webservices.lyrdb.com/getlyr.php?q=" & id)
                        Dim lyrics As String = client.DownloadString(lyricsUrl).Replace(ControlChars.CrLf, ControlChars.Lf)
                        Dim lyricsClean As String = lyrics.Remove(lyrics.LastIndexOf("<")).TrimEnd
                        If lyricsClean <> [String].Empty Then
                            Return lyricsClean
                        End If
                    Next
                    Return [String].Empty
                Catch WebException As Exception
                    Return [String].Empty
                End Try
                Return [String].Empty
            ElseIf My.Settings.LyricsService = "LeosLyrics" And CheckConn.IsConnected = True Then
                Dim ly As New Playme.LyricGrabbers.LeosLyricsGrabber()
                ly.Enabled = True
                Return ly.Execute(context)
            ElseIf My.Settings.LyricsService = "WowLyrics" And CheckConn.IsConnected = True Then
                Dim ly As New Playme.LyricGrabbers.WowLyricsGrabber()
                ly.Enabled = True
                Return ly.Execute(context)
            ElseIf My.Settings.LyricsService = "LyricsPlugin" And CheckConn.IsConnected = True Then
                Dim ly As New Playme.LyricGrabbers.LyricsPluginGrabber()
                ly.Enabled = True
                Return ly.Execute(context)
            ElseIf My.Settings.LyricsService = "LyrDB2" And CheckConn.IsConnected = True Then
                Dim ly As New Playme.LyricGrabbers.LyrDBGrabber
                ly.Enabled = True
                Return ly.Execute(context)
            ElseIf My.Settings.LyricsService = "Global" Then
                Dim x As String
                If String.IsNullOrEmpty(context.Tag.Lyrics) = False And Fetch = False Then
                    Return context.Tag.Lyrics
                Else
                    Dim ly As New Playme.LyricGrabbers.LyricsPluginGrabber() With {.Enabled = True}
                    x = ly.Execute(context)
                    If String.IsNullOrEmpty(x) = False Then
                        Return x
                    Else
                        Dim lx As New Playme.LyricGrabbers.LyrDBGrabber() With {.Enabled = True}
                        x = lx.Execute(context)
                        If String.IsNullOrEmpty(x) = False Then
                            Return x
                        Else
                            Dim lu As New Playme.LyricGrabbers.WowLyricsGrabber() With {.Enabled = True}
                            x = lu.Execute(context)
                            If String.IsNullOrEmpty(x) = False Then
                                Return x
                            Else
                                Dim li As New Playme.LyricGrabbers.LeosLyricsGrabber() With {.Enabled = True}
                                x = li.Execute(context)
                                If String.IsNullOrEmpty(x) = False Then
                                    Return x
                                Else
                                    Return "No Lyrics Found"
                                End If
                            End If
                        End If
                    End If
                End If
            Else
                Return "No Lyrics Found"
            End If
        Else
            Return "No Lyrics Found"
        End If
    End Function

    Public Sub SaveLyricsTag(ByVal context As TagLib.File, ByVal lyrics As String)
        context.Tag.Lyrics = lyrics
        If frmMain.ctlPlayer.State = VideoLan.VlcState.Playing Then
            frmMain.SaveTag = True
        Else
            Dim instance As New SaveLyricThread
            instance._Tag = context
            Dim th As New Threading.Thread(AddressOf instance.Save)
            th.Start()
        End If
    End Sub

    Public Sub ClearLyricsTag(ByVal context As TagLib.File)
        context.Tag.Lyrics = ""
        If frmMain.ctlPlayer.State = VideoLan.VlcState.Playing Then
            frmMain.SaveTag = True
        Else
            Dim instance As New ClearLyricThread
            instance._Tag = context
            Dim th As New Threading.Thread(AddressOf instance.Save)
            th.Start()
        End If
    End Sub
End Module

Public Class SaveLyricThread

    Private tag As TagLib.File
    Public Property _Tag() As TagLib.File
        Get
            Return tag
        End Get
        Set(ByVal value As TagLib.File)
            tag = value
        End Set
    End Property

    Public Sub Save()
        Dim temp As Boolean = True
        Do While temp = True
            Try
                _Tag.Save()
                temp = False
            Catch IOException As Exception
                temp = True
            End Try
        Loop
    End Sub


End Class

Public Class ClearLyricThread

    Private tag As TagLib.File
    Public Property _Tag() As TagLib.File
        Get
            Return tag
        End Get
        Set(ByVal value As TagLib.File)
            tag = value
        End Set
    End Property

    Public Sub Save()
        Dim temp As Boolean = True
        Do While temp = True
            Try
                _Tag.Save()
                temp = False
            Catch IOException As Exception
                temp = True
            End Try
        Loop
    End Sub


End Class
